package com.ibigcat.module.mdm.controller.admin.platform;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.ibigcat.framework.common.pojo.PageParam;
import com.ibigcat.framework.common.pojo.PageResult;
import com.ibigcat.framework.common.pojo.CommonResult;
import com.ibigcat.framework.common.util.object.BeanUtils;
import static com.ibigcat.framework.common.pojo.CommonResult.success;

import com.ibigcat.framework.excel.core.util.ExcelUtils;

import com.ibigcat.framework.apilog.core.annotation.ApiAccessLog;
import static com.ibigcat.framework.apilog.core.enums.OperateTypeEnum.*;

import com.ibigcat.module.mdm.controller.admin.platform.vo.*;
import com.ibigcat.module.mdm.dao.entity.PlatformDO;
import com.ibigcat.module.mdm.service.PlatformService;

@Tag(name = "管理后台 - 平台管理")
@RestController
@RequestMapping("/mdm/platform")
@Validated
public class PlatformController {

    @Resource
    private PlatformService platformService;

    @PostMapping("/create")
    @Operation(summary = "创建平台管理")
    @PreAuthorize("@ss.hasPermission('mdm:platform:create')")
    public CommonResult<Long> createPlatform(@Valid @RequestBody PlatformSaveReqVO createReqVO) {
        return success(platformService.createPlatform(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新平台管理")
    @PreAuthorize("@ss.hasPermission('mdm:platform:update')")
    public CommonResult<Boolean> updatePlatform(@Valid @RequestBody PlatformSaveReqVO updateReqVO) {
        platformService.updatePlatform(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除平台管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mdm:platform:delete')")
    public CommonResult<Boolean> deletePlatform(@RequestParam("id") Long id) {
        platformService.deletePlatform(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得平台管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mdm:platform:query')")
    public CommonResult<PlatformRespVO> getPlatform(@RequestParam("id") Long id) {
        PlatformDO platform = platformService.getPlatform(id);
        return success(BeanUtils.toBean(platform, PlatformRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得平台管理分页")
    @PreAuthorize("@ss.hasPermission('mdm:platform:query')")
    public CommonResult<PageResult<PlatformRespVO>> getPlatformPage(@Valid PlatformPageReqVO pageReqVO) {
        PageResult<PlatformDO> pageResult = platformService.getPlatformPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, PlatformRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出平台管理 Excel")
    @PreAuthorize("@ss.hasPermission('mdm:platform:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPlatformExcel(@Valid PlatformPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PlatformDO> list = platformService.getPlatformPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "平台管理.xls", "数据", PlatformRespVO.class,
                        BeanUtils.toBean(list, PlatformRespVO.class));
    }

}